﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using vngoodlife.Singleton;

namespace vngoodlife.Models
{
    public class RelatedArticleModels
    {
        internal bool DeleteRelatedArticle(int articleID, int relatedArticleID)
        {
            try
            {
                var related = DataSingleton.Instance.GetLinQContext().Related_Articles
                        .Where(u => 
                                (u.ArticleID == articleID && u.RelatedArticleID == relatedArticleID)
                                || (u.ArticleID == relatedArticleID && u.RelatedArticleID == articleID)
                               );
                DataSingleton.Instance.GetLinQContext().Related_Articles.DeleteAllOnSubmit(related);
                DataSingleton.Instance.GetLinQContext().Related_Articles.Context.SubmitChanges();
            }
            catch (Exception)
            {
                DataSingleton.Instance.RefreshInstance();
                return false;
            }
            return true;
        }

        internal void InsertRelatedArticle(Related_Article related)
        {
            try
            {
                if (!DataSingleton.Instance.GetLinQContext().Related_Articles.Any(u => u == related))
                {
                    DataSingleton.Instance.GetLinQContext().Related_Articles.InsertOnSubmit(related);
                    DataSingleton.Instance.GetLinQContext().Related_Articles.Context.SubmitChanges();
                }
            }
            catch (Exception)
            {
                DataSingleton.Instance.RefreshInstance();
            }
        }

        internal bool InsertManyRelatedArticle(int id, string relatedArticleID)
        {
            try
            {
                DataSingleton.Instance.GetLinQContext().sp_RelatedArticleInsert(id, relatedArticleID);
            }
            catch (Exception)
            {
                DataSingleton.Instance.RefreshInstance();
                return false;
            }
            return true;
        }
    }
}